import requests
from lxml import etree
url='https://datachart.500.com/ssq/'
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0'}
resp=requests.get(url,headers=headers)
resp.encoding='gbk'

e=etree.HTML(resp.text)
# //tbody[@id="tdata"]/tr/td[contains(@class,'chartBall01')]/text()   每一行的红色球
# //tbody[@id="tdata"]/tr[not(contains(@class,'tdbck'))]              所有行红色球

#创建列表的4种方式_推导式创建列表
# b = [x*2 for x in range(100) if x%9==0]
#在每一行中查找class是chartBall01的内容                                     所有行但不包括class是tdbck的行
reds=[tr.xpath('./td[contains(@class,"chartBall01")]/text()')for tr in e.xpath('//tbody[@id="tdata"]/tr[not(contains(@class,"tdbck"))]')]
blues=e.xpath('//tbody[@id="tdata"]/tr[not(contains(@class,"tdbck"))]/td[contains(@class,"chartBall02")]/text()')
# print(reds,blues)
for r,b in zip(reds,blues):
    print(f"红球是{r},蓝球是{b}")
